<?php

/**
 *
 * @file   Index.php
 * @date   2017-11-28
 * @author sg
 */

namespace app\admin\controller;
use app\admin\model\StockApply;
use app\admin\model\Company;
use app\admin\model\CompanyIncome;
use app\admin\model\CompanyMoneyAllot;
use app\admin\model\FilialeTodoModel;
class Index extends Common{

    public function _initialize(){
        // todo...
    }
    /**
     * 后台首页
     */
    public function index(){

        $stockapply = new StockApply();
        //获取股东数量
        $stockCount = $stockapply->where('CompanyId',session('company_id'))->where('approvestateid',4)->count();

        $whereCity = [];
        $whereCounty = [];
        $whereTown = [];
        $company_type = session('company_type');
        if($company_type == 1)
        {
            $province_id = session('province_id');
            $whereCity['provinceid'] = $province_id;
            $whereCounty['provinceid'] = $province_id;
            $whereTown['provinceid'] = $province_id;
        }
        else if($company_type == 2)
        {
            $city_id = session('city_id');
            $whereCity['cityid'] = $city_id;
            $whereCounty['cityid'] = $city_id;
            $whereTown['cityid'] = $city_id;
        }
        else
        {
            $county_id = session('county_id');
            $whereCity['countyid'] = $county_id;
            $whereCounty['countyid'] = $county_id;
            $whereTown['countyid'] = $county_id;
        }
        $whereCity['companytypeid'] = 2;
        $whereCounty['companytypeid'] = 3;
        $whereTown['companytypeid'] = 4;

        $CompanyModel = new Company();

        //获取市公司数量
        $CompanyCityCount = $CompanyModel->where($whereCity)->count();
        //获取县区公司数量
        $CompanyCountyCount = $CompanyModel->where($whereCounty)->count();
        //获取惠民店数量
        $CompanyBshopCount = $CompanyModel->where($whereTown)->count();


        $CompanyData = $CompanyModel->findData(['id'=>session('company_id')],'money,createon,allot_percent,last_allot_time');//获取分公司总收益
        if($CompanyData['last_allot_time'] == null)
        {
            $Stime = $CompanyData['createon'];
        }
        else
        {
            $Stime = $CompanyData['last_allot_time'];
        }
        $CompanyMoneyAllotEtime = date("Y-m-d H:i:s");

        $companyMoney_Model = new CompanyIncome();
        $wheredata = array();
        $wheredata['cpid'] = array('eq',session('company_id'));
        $wheredata['addtime'] = array(array('egt',$Stime),array('elt',$CompanyMoneyAllotEtime));
        $wheredata['source'] = array('in','1,2');
        //获取可分红的销售总额
        $company_money = $companyMoney_Model->sumData($wheredata,'money');
        //获取分红比例
        $conpany_allot_percent = $CompanyData['allot_percent'];
        $conpany_allot_percent = $conpany_allot_percent / 100;
        //分公司可分红金额
        $company_bonus = number_format($company_money * $conpany_allot_percent * 0.49,2);

        $this->assign('stockCount',$stockCount);
        $this->assign('company_money',number_format($CompanyData['money'],2));
        $this->assign('company_bonus',$company_bonus);
        $this->assign('companyCityCount',$CompanyCityCount);
        $this->assign('companyCountyCount',$CompanyCountyCount);
        $this->assign('companyBshopCount',$CompanyBshopCount);


        $filialeTodoModel = new FilialeTodoModel();
        $filialeTodoData =  $filialeTodoModel->selectData(['cpid'=>session('company_id'),'manageid'=>session('user_id'),'state'=>0]);
        $this->assign('filialeTodoList',$filialeTodoData);
        return $this->fetch();
    }
}